package com.minelx.statementsplus;

import com.minelx.statementsplus.conditions.ConditionsPlus;
import com.minelx.statementsplus.split.LongArraySplitter;
import com.minelx.statementsplus.split.ProxiesSplitter;
import com.minelx.statementsplus.split.StringArraySplitter;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;

import static java.util.stream.Collectors.joining;

public class Majors {
	public static final int MEMBER_ID__FIT_PARTITION_LENGTH = 500;

	private static final int FLOWS_MEMBER_ID__FIT_PARTITION_LENGTH = 10;
	
	private static final MajorSqlHeadFactory generalHeadFactory;

	static {
		generalHeadFactory = MajorSqlHeadFactory.general();
	}

	private Majors() {
	}

	// FIXME 2021/8/30  wait for me!!!      name -> MajorSplitter  splitter should be schematized in major_sql_head.json
	public static MajorSql memberIds(long[] memberIds) {
		String memberIdsAsText = Arrays.stream(memberIds).mapToObj(String::valueOf).collect(joining(", "));
		return MajorSql.of(
				generalHeadFactory.createMajorSqlHead("100商户号", Collections.singletonMap("memberIds", memberIdsAsText)),
				new LongArraySplitter(memberIds, MEMBER_ID__FIT_PARTITION_LENGTH, Majors::memberIds)
		);
	}

	public static MajorSql proxies(int[] proxyIds) {
		return MajorSql.of(
				generalHeadFactory.createMajorSqlHead("地市", Collections.singletonMap("proxiesCondition", ConditionsPlus.proxies(proxyIds).toText())),
				new ProxiesSplitter(proxyIds, Majors::proxies)
		);
	}

	public static MajorSql flows(String databaseIndex, String date, long[] memberIds) {
		String memberIdsAsText = Arrays.stream(memberIds).mapToObj(String::valueOf).collect(joining(","));
		HashMap<String, String> parameters = new HashMap<>();
		parameters.put("databaseName", databaseIndex);
		parameters.put("date", date);
		parameters.put("memberIds", memberIdsAsText);
		return MajorSql.of(
				generalHeadFactory.createMajorSqlHead("流水", parameters),
				new LongArraySplitter(memberIds, FLOWS_MEMBER_ID__FIT_PARTITION_LENGTH, partition -> flows(databaseIndex, date, partition))
		);
	}

	public static MajorSql hlbMerchantId(String[] merchantIds) {
		String merchantIdsAsText = Arrays.stream(merchantIds).map(item -> "\"" + item + "\"").collect(joining(","));
		return MajorSql.of(
				generalHeadFactory.createMajorSqlHead("合利宝商户号", Collections.singletonMap("hlbMerchantIds", merchantIdsAsText)),
				new StringArraySplitter(merchantIds, Majors::hlbMerchantId)
		);
	}

	public static MajorSql wechatIds(String[] ids) {
		String wechatIds = Arrays.stream(ids).map(item -> "\"" + item + "\"").collect(joining(","));
		return MajorSql.of(
				generalHeadFactory.createMajorSqlHead("微信间连号", Collections.singletonMap("wechatIds", wechatIds)),
				new StringArraySplitter(ids, Majors::wechatIds)
		);
	}
}
